import O1HD from "../../engines/gameEngine";
import PageBase from "../Base/PageBase";
import { IPagePramsBase, IUnlockPagePrams } from "../Interface";
import mainRoot from "../MainRoot";

const {ccclass, property} = cc._decorator;

@ccclass
export default class UnLockPage extends PageBase {

    private unLockOneCall:()=>void=null;

    private unlockAllCall:()=>void=null;

    private cancel:()=> void = null;

    private isShowOne: boolean = null;

    public static unlockAllTime:number=0;
    @property({type: cc.Label, displayName: '视频观看次数'})
    lbCount: cc.Label = null;
    @property({type: cc.Node, displayName: "当前解锁按钮"})
    curLevelNode: cc.Node = null;



    async OnPageOpen(prams: IUnlockPagePrams){
        super.OnPageOpen(prams);
        this.unLockOneCall=prams.unlockCall;
        this.unlockAllCall=prams.allCall;
        this.isShowOne = prams.isShowOne;
        this.cancel = prams.cancel;
    }

    protected update(dt: number): void {
        this.showUnLockPage();
    }


    showUnLockPage() {
       this.lbCount.string="("+UnLockPage.unlockAllTime+"/3)次";
       this.curLevelNode.active=this.isShowOne;

    }


    onLookCurLevel() {
        O1HD.getManagers().soundMgr.playSound("mainSound/anNiu");
        if(O1HD.getServices().userSrv.getCoins() >0){
            O1HD.getServices().userSrv.setCoins(-1,true);
            this.unLockOneCall&&this.unLockOneCall()
            this.ClosePage()
        }else{
            O1HD.getServices().miniGameSrv.getAppApiInstance().loadAndShowVideoAd(()=>{
             this.unLockOneCall&&this.unLockOneCall()
             this.ClosePage()
            },()=>{
     
            },null,'看视频解锁关卡')
        }
    }

    onLookAllLevel() {
        O1HD.getManagers().soundMgr.playSound("mainSound/anNiu");
        if(O1HD.getServices().userSrv.getCoins() >0){
            O1HD.getServices().userSrv.setCoins(-1,true);
            UnLockPage.unlockAllTime++;
                if(UnLockPage.unlockAllTime>=3)
                {
                    this.unlockAllCall&&this.unlockAllCall()
                    this.ClosePage();
                }
        }else{
            O1HD.getServices().miniGameSrv.getAppApiInstance().loadAndShowVideoAd(()=>{
                UnLockPage.unlockAllTime++;
                if(UnLockPage.unlockAllTime>=3)
                {
                    this.unlockAllCall&&this.unlockAllCall()
                    this.ClosePage();
                }
            },()=>{
        
            },null,'看视频解锁所有关卡')
        }
    }

    OnCloseBtn(): void {
        this.cancel&&this.cancel();
        super.OnCloseBtn();
    }


}
